<?php
/**
 * 登录控制类
 * @author 大梦
 * @DateTime 2019/07/06
 */

namespace app\index\controller;

use think\Request;
use app\common\model\Staff as StaffModel;

class Login extends Common
{
    public function index()
    {
        return view();
    }

    public function login()
    {
        $params = input();
        $nickname = $params['nickname'] ?? '';

        $where_user = [
            ['status', 'eq', 1],
            ['is_del', 'eq', 0],
        ];

        $where_or = [
            ['nickname', 'eq', $nickname],
            ['username', 'eq', $nickname],
        ];

        $StaffModel = new StaffModel();
        $userInfo = $StaffModel
            ->where($where_user)
            ->where(function ($q) use($where_or) {
                $q->whereOr($where_or);
            })
            ->find();

        if ($userInfo) {
            session('member_id', $userInfo['id']);
            return $this->api_success('登录成功', $userInfo);
        }
        return $this->api_error('用户不存在');
    }

    public function logout()
    {
        session('member_id', null);
        return $this->api_success('退出成功');
    }
}
